{include file='common/head' /}
　　
<script src="__LAYUI__/jq.js"></script>
<body>
<!--说明栏目-->
<div class="x-nav">
              <span class="layui-breadcrumb">
                  <a><cite>广告管理</cite></a>
                  <a><cite>广告列表</cite></a>
              </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<!--主体开始-->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <!--搜索-->
                <div class="layui-card-body ">
                    <form class="layui-form">
                        <!--名称-->
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="title" placeholder="标题" autocomplete="off" class="layui-input"
                                   value="{$get.title|default=''}">
                        </div>
                        <!--小程序跳转地址-->
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="url" placeholder="小程序跳转地址" autocomplete="off" class="layui-input"
                                   value="{$get.url|default=''}">
                        </div>
                        <!--app跳转地址-->
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="app_url" placeholder="app跳转地址" autocomplete="off"
                                   class="layui-input" value="{$get.app_url|default=''}">
                        </div>
                        <!--                                类型-->
                        <div class="layui-inline layui-show-xs-block">
                            <select name="type_key">
                                <option value="">广告类型</option>
                                {volist name="type" id="v"}
                                <option value="{$key}" {if condition="$get['type_key'] == $key" }selected{
                                /if}>{$v}</option>
                                {/volist}
                            </select>
                        </div>
                        <!--显示-->
                        <div class="layui-inline layui-show-xs-block">
                            <select name="show">
                                <option value="" selected>是否显示</option>
                                <option value="0" {if
                                        condition="isset($get['show']) && strlen($get['show']) >= 1 && $get['show'] == 0"
                                        }selected{
                                /if}>隐藏</option>
                                <option value="1" {if condition="$get['show'] == 1" }selected{
                                /if}>显示</option>
                            </select>
                        </div>
                        <!--跳转-->
                        <div class="layui-inline layui-show-xs-block">
                            <select name="is_skip">
                                <option value="" selected>是否跳转</option>
                                <option value="0" {if
                                        condition="isset($get['is_skip']) && strlen($get['is_skip']) >= 1 && $get['is_skip'] == 0"
                                        }selected{
                                /if}>不跳转</option>
                                <option value="1" {if condition="$get['is_skip'] == 1" }selected{
                                /if}>跳转</option>
                            </select>
                        </div>
                        <!-- 分页数据 -->
                        <div class="layui-inline layui-show-xs-block" style="width: 150px;">
                            <select name="limit">
                                <option value="" selected>分页数据</option>
                                <option value="10" {if condition="$get['limit'] == 10" }selected{
                                /if}>10条</option>
                                <option value="20" {if condition="$get['limit'] == 20" }selected{
                                /if}>20条</option>
                                <option value="30" {if condition="$get['limit'] == 30" }selected{
                                /if}>30条</option>
                                <option value="50" {if condition="$get['limit'] == 50" }selected{
                                /if}>50条</option>
                                <option value="100" {if condition="$get['limit'] == 100" }selected{
                                /if}>100条</option>
                                <option value="150" {if condition="$get['limit'] == 150" }selected{
                                /if}>150条</option>
                                <option value="200" {if condition="$get['limit'] == 200" }selected{
                                /if}>200条</option>
                            </select>
                        </div>
                        <!--按钮-->
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach"><i
                                    class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <!--按钮和功能-->
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="xadmin.open('添加广告列表','/admin/advert/ad_edit',960,660)"><i
                            class="layui-icon"></i>添加
                    </button>
                    <button class="layui-btn layui-btn-normal" title="点我批量改变" onclick="pinLiangStatusBtn('show')">
                        批量显示/隐藏
                    </button>
                    <button class="layui-btn layui-btn-normal" title="点我批量改变" onclick="pinLiangStatusBtn('is_skip')">
                        批量跳转/不跳转
                    </button>
                    <span style="line-height:40px;float: right;">共有数据：{$count} 条</span>
                </div>
                <!--表格数据展示-->
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th style="text-align: center;width: 3%">
                                <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                            </th>
                            <th style="text-align: center;">标题</th>
                            <th style="text-align: center;">图片</th>
                            <th style="text-align: center;">类型</th>
                            <th style="text-align: center;">排序</th>
                            <th style="text-align: center;">是否跳转</th>
                            <th style="text-align: center;">小程序跳转地址</th>
                            <th style="text-align: center;">app跳转地址</th>
                            <th style="text-align: center;">是否显示</th>
                            <th style="text-align: center;">操作时间</th>
                            <th style="text-align: center;">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {volist name="list" id="v"}
                        <tr>
                            <td style="text-align: center;">
                                <input type="checkbox" name="id" value="{$v.id}" lay-skin="primary">
                            </td>
                            <td style="text-align: center;">
                                <span onclick="copyCode('{$v.title}')">{$v.title}</span>
                            </td>
                            <td style="text-align: center;"><img style="width:200px;cursor: pointer;" alt="图片地址错误"
                                                                 src="{$v.img_url}" title="带我预览图片"
                                                                 onclick="lookImg('{$v.img_url}','-首页广告图-{$v.title}')"/>
                            </td>
                            <td>{if isset($type[$v.type_key])}{$type[$v.type_key]}{/if}</td>
                            <td style="text-align: center;">
                                <span class="sortClick" title="排序"
                                      style="cursor: pointer; color:steelblue;">{$v.sort}</span>
                                <div style="display: none;">
                                    <input type="number" value="{$v.sort}" class="layui-input" placeholder="输入排序值"
                                           style="width: 60px;">
                                    <button data-id="{$v.id}" title="点我改变" type="button"
                                            class="sortBtn layui-btn layui-btn-sm layui-btn-normal"><i
                                            class="layui-icon layui-icon-edit"></i></button>
                                </div>
                            </td>
                            <!--是否跳转-->
                            <td class="clickStatus" style="text-align: center;" data-id={$v.id}
                                data-title="{$v.is_skip == 1 ? '不跳转' : '跳转'}" data-key="is_skip"
                                data-val="{$v.is_skip == 1 ? 0 : 1}">
                                {$v.is_skip == 1 ?
                                '
                                <button type="button" title="点我更改状态" class="layui-btn layui-btn-normal layui-btn-xs"><i
                                        class="layui-icon ">&#xe605;</i></button>
                                ' :
                                '
                                <button type="button" title="点我更改状态" class="layui-btn layui-btn-danger layui-btn-xs"><i
                                        class="layui-icon">&#x1006;</i></button>
                                ';
                                }
                            </td>
                            <td style="text-align: center;">
                                <span onclick="copyCode('{$v.url}')">{$v.url}</span>
                            </td>
                            <td style="text-align: center;">
                                <span onclick="copyCode('{$v.app_url}')">{$v.app_url}</span>
                            </td>
                            <!-- 显示 -->
                            <td class="clickStatus" style="text-align: center;" data-id={$v.id}
                                data-title="{$v.show == 1 ? '隐藏' : '显示'}" data-key="show"
                                data-val="{$v.show == 1 ? 0 : 1}">
                                {$v.show == 1 ?
                                '
                                <button type="button" title="点我更改状态" class="layui-btn layui-btn-normal layui-btn-xs"><i
                                        class="layui-icon ">&#xe605;</i></button>
                                ' :
                                '
                                <button type="button" title="点我更改状态" class="layui-btn layui-btn-danger layui-btn-xs"><i
                                        class="layui-icon">&#x1006;</i></button>
                                ';
                                }
                            </td>
                            <td style="text-align: center;">{:date('Y-m-d H:i:s',$v['add_time'])}</td>
                            <td style="text-align: center;" class="td-manage">
                                <button class="layui-btn layui-btn layui-btn-xs" title="编辑"
                                        onclick="xadmin.open('编辑','/admin/advert/ad_edit?id={$v.id}',960,660)"
                                        href="javascript:;">
                                    <i class="layui-icon">&#xe642;</i>编辑
                                </button>
                                <br>
                                <button class="layui-btn-danger layui-btn layui-btn-xs" title="删除"
                                        onclick="member_del(this,{$v['id']})" href="javascript:;">
                                    <i class="layui-icon">&#xe640;</i>删除
                                </button>
                            </td>
                        </tr>
                        {/volist}
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page">
                        {$page}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form;
// 监听全选
        form.on('checkbox(checkall)', function (data) {
            if (data.elem.checked) {
                $('tbody input').prop('checked', true);
            } else {
                $('tbody input').prop('checked', false);
            }
            form.render('checkbox');
        });
    });

    // 前缀请求地址
    var PREFIX_AJAX_URL = '/admin/advert/ad_';

    // 状态值
    var objKeys = {
        // 显示
        show: {  // 下标  0 表示 隐藏  1 显示
            val: [0, 1],
            title: ['隐藏', '显示']
        },
        // 是否跳转
        is_skip: {  // 下标    1 跳转  0 表示 不跳转
            val: [0, 1],
            title: ['不跳转', '跳转']
        },
    }

    var btnStrs = [
        '<button type="button" title="点我更改状态" class="layui-btn layui-btn-danger layui-btn-xs"><i class="layui-icon">ဆ</i></button>', // 不开启
        '<button type="button" title="点我更改状态" class="layui-btn layui-btn-normal layui-btn-xs"><i class="layui-icon "></i></button>' // 开启
    ];


    $(function () {
        bindClickBtn()
        $(".sortClick").click(function () {
            $(this).parent().find('div').css('display', 'flex')
            $(this).hide()
            $(this).parent().find('input').focus();
        })

        $(".sortBtn").click(function () {
            var id = $(this).attr('data-id')
            if (!id) return layer.msg('没有该选项', {icon: 5})
            var sort = $(this).prev().val()
            if (!sort || sort < 0) return layer.msg('排序输入错误', {icon: 5})
            var oldSort = $(this).parent().parent().find('.sortClick').text()
            if (oldSort - 0 == sort - 0) return layer.msg('值不能相同', {icon: 5})
            var self = this
            $.ajax({
                url: PREFIX_AJAX_URL + 'sortEdit',
                data: {id, sort},
                dataType: 'json',
                success: function (res) {
                    layer.msg(res.msg || '操作ok', {icon: res.code == 200 ? 6 : 5})
                    if (res && res.data) {
                        $(self).parent().parent().find('span').text(sort).show(300)
                        $(self).parent().css('display', 'none')
                    }
                },
                error: function (err) {
                    layer.msg('输入错误', {icon: 5})
                }
            })
        })
    })

    // 绑定事件
    function bindClickBtn() {
        $("table tbody .clickStatus button").off('click');
        $("table tbody .clickStatus button").click(function (e) {
            var self = this;
            // 拿取到tb
            var tb = $(this).parent()
            var data = {
                id: $(tb).attr('data-id'),
                val: $(tb).attr('data-val'),
                key: $(tb).attr('data-key')
            }
            clickStatusBtn(PREFIX_AJAX_URL + '_status.html', data, {
                msg: '此商品序号:' + data.id + "-状态为:" + $(tb).attr('data-title') + '？',
                elem: self
            }, toggleBtn);
        })
    }

    /**
     *  将按钮更改为相反的状态
     */
    function toggleBtn(data, self) {
        if (self && objKeys[data.key]) {
            var i = objKeys[data.key].val.indexOf(data.val - 0)
            if (i >= 0) {
                var tb = $(self).parent()
                $(self).hide(500, function () {
                    $(tb).html(btnStrs[i])
                    setTimeout(function () {
                        $(self).show(400, function () {
                            bindClickBtn();
                        })
                    }, 300)
                })
                // 目前只有两种状态
                $(tb).attr('data-val', objKeys[data.key].val[i == 0 ? 1 : 0])
                $(tb).attr('data-title', objKeys[data.key].title[i == 0 ? 1 : 0])
            }
        }
    }


    //批量删除
    function delAll(argument) {
        var ids = [];
        // 获取选中的id
        $('tbody input[type="checkbox"]').each(function (index, el) {
            if ($(this).prop('checked')) {
                ids.push($(this).val())
            }
        });

        if (ids.length == 0 || !ids) return layer.msg('必须选择一项', {icon: 5});

        // 弹窗提示中
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                url: PREFIX_AJAX_URL + "delAll",
                type: "post",
                data: {id: ids},
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        //捉到所有被选中的，发异步进行删除
                        layer.msg('删除成功', {icon: 1, time: 1000});
                        $(".layui-form-checked").not('.header').parents('tr').remove();
                    } else {
                        layer.alert(res.msg, {icon: 2, time: 1000});
                    }
                },
                error: function (e) {
                    layer.alert("网络错误", {icon: 5, time: 1000});
                },
            });
        });
    }

    // 三个按钮操作 等
    function pinLiangStatusBtn(key) {
        if (!objKeys[key]) return layer.msg('必须选择一项', {icon: 5});
        var ids = [];
        // 获取选中的id
        $('tbody input[type="checkbox"]').each(function (index, el) {
            if ($(this).prop('checked')) {
                ids.push($(this).val())
            }
        });
        if (!ids || ids.length == 0) return layer.msg('必须选择一项', {icon: 4});
        // 数据
        var obj = {
            ids,
            key
        }
        layer.confirm('确认批量操作？', {
            title: '批量操作',
            icon: 3,
            btn: [objKeys[key].title[1], objKeys[key].title[0], '取消']
        }, function (index) {
            obj.val = objKeys[key].val[1]
            ajaxFun(PREFIX_AJAX_URL + 'pinliang_status.html', obj, 'post', function () {
                window.location.href = window.location.href
            })
            layer.close(index)
        }, function (index) {
            obj.val = objKeys[key].val[0]
            ajaxFun(PREFIX_AJAX_URL + 'pinliang_status.html', obj, 'post', function () {
                window.location.href = window.location.href
            })
        })
    }


    // ajax封装
    function ajaxFun(url, obj, type = 'post', fun, errFun, elem) {
        var s = layer.msg('加载中...', {icon: 4, time: 999999})
        $.ajax({
            url,
            type,
            data: obj,
            dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg ? res.msg : '操作ok', {icon: 1, time: 1000});
                } else {
                    layer.alert(res.msg, {icon: 2, time: 1000});
                }
                layer.close(s)
                if (fun) fun(res.data, elem);
            },
            error: function (e) {
                layer.close(s)
                layer.alert("网络错误", {icon: 5, time: 1000});
                if (errFun) errFun(res);
            },
        });
    }


    /**
     * 封装弹窗中的数据信息
     * 参数：  请求地址， 请求数据， 标题，成功回调函数，错误回调函数
     */
    function clickStatusBtn(url, obj, options, fun = false, errFun = false) {
        var s = layer.msg('加载中...', {icon: 4, time: 999999})
        layer.confirm(options.msg, {
            title: options.title ? options.title : '操作提示',
            icon: options.icon ? options.icon : 3
        }, function (index) {
            $.ajax({
                url,
                type: "post",
                data: obj,
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        //捉到所有被选中的，发异步进行删除
                        layer.msg(res.msg ? res.msg : '操作ok', {icon: 1, time: 1000});
                        //$(".layui-form-checked").not('.header').parents('tr').remove();
                    } else {
                        layer.alert(res.msg, {icon: 2, time: 1000});
                    }
                    layer.close(s)
                    if (fun) fun(res.data, options.elem);
                },
                error: function (e) {
                    layer.close(s)
                    layer.alert("网络错误", {icon: 5, time: 1000});
                    if (errFun) errFun(res);
                },
            });
        });
    }


    // 请求地址前缀
    var url_prefix = '/admin/setting/bannerLive'

    /*删除*/
    function member_del(obj, id) {
        layer.confirm('确认要删除吗？', {icon: 8, title: '提示'}, function (index) {
            var ids = [];
            ids.push(id);
            $.ajax({
                url: "{:url('Advert/ad_del')}",
                type: "post",
                data: {id: ids},
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg('删除成功', {icon: 1, time: 1000});
                        $(obj).parents("tr").remove();
                    } else {
                        layer.alert(res.msg, {icon: 2, time: 1000});
                    }
                },
                error: function (e) {
                    layer.alert("网络错误", {icon: 5, time: 1000});
                },
            });
        }, function () {
            layer.msg('取消成功', {icon: 6, time: 1000});
        });
    }
</script>
{include file='common/copyCode' /}
{include file='common/scroll' /}
{include file='common/footer' /}